<?php
/*************************************************************************************

   Google Maps Module by breitsch (mail@casa-loca.com)

*************************************************************************************/

// load general configuration
$path = dirname(dirname(dirname(realpath(dirname(__FILE__)))));
require_once ($path."/config/phpwcms/conf.inc.php");

// Get parameters from URL
(preg_match('/^[0-9][0-9,]*[0-9]*$/', $_GET["cpids"])) ? $cp_ID = $_GET["cpids"] : die($_SERVER['PHP_SELF']);
(is_numeric($_GET["radius"])) ?	$radius = $_GET["radius"] :	$radius = 0;
(is_numeric($_GET["lat"])) ?	$center_lat = $_GET["lat"] :	$center_lat = 0;
(is_numeric($_GET["lng"])) ?	$center_lng = $_GET["lng"] :	$center_lng = 0;
($_GET["sys"]==1) ? $distUnit = 6371 : $distUnit = 3959 ;
if(isset($_GET["txt"])){
  $txt =  utf8_decode ( $_GET["txt"]);
  $txt = ( get_magic_quotes_gpc ()) ? stripslashes($txt) : $txt;
}

// Start XML file, create parent node
if (version_compare(phpversion(), "5.0.0") == -1) {
  require_once("DOMAdapter.inc");
  header("Content-Type: text/xml");
  $xml = new DOMAdapter();
  $dom = $xml->createDocument("markers");
  $dom->documentElement->setAttribute("id", "listing");
} else {
  header("Content-Type: text/xml");
  $dom = new DOMDocument('1.0', 'UTF-8');
  $node = $dom->createElement("markers");
  $parnode = $dom->appendChild($node);
}
//$dom = new DOMDocument('1.0', 'UTF-8');
//$node = $dom->createElement("markers");
//$parnode = $dom->appendChild($node);

// Opens a connection to a mySQL server
$connection=mysql_connect ($phpwcms['db_host'], $phpwcms['db_user'], $phpwcms['db_pass']);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($phpwcms['db_table'], $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}

$sqltxt ='';
if($txt) {
  $sqltxt = " (googlemaps_title LIKE ''";
  if (substr($_GET['srch'], 0, 1)==1) $sqltxt .= " OR googlemaps_title LIKE '%".mysql_real_escape_string($txt, $connection)."%'";
  if (substr($_GET['srch'], 1, 1)==1) $sqltxt .= " OR googlemaps_text LIKE '%".mysql_real_escape_string($txt, $connection)."%'";
  if (substr($_GET['srch'], 2, 1)==1) $sqltxt .= " OR googlemaps_searchlisting LIKE '%".mysql_real_escape_string($txt, $connection)."%'";  
  $sqltxt .= ") AND";
}

if($radius) {
  $query = "SELECT googlemaps_id, googlemaps_searchlisting, googlemaps_title, googlemaps_lat, googlemaps_lon, googlemaps_status, ( $distUnit * acos( cos( radians('$center_lat') ) * cos( radians( googlemaps_lat ) ) * cos( radians( googlemaps_lon ) - radians('$center_lng') ) + sin( radians('$center_lat') ) * sin( radians( googlemaps_lat ) ) ) ) AS distance FROM phpwcms_googlemaps WHERE googlemaps_id IN($cp_ID) AND".$sqltxt." googlemaps_status=1 HAVING distance < '$radius' ORDER BY distance";
}else{
  $query = "SELECT googlemaps_id, googlemaps_searchlisting, googlemaps_title, googlemaps_lat, googlemaps_lon, googlemaps_status FROM phpwcms_googlemaps WHERE googlemaps_id IN($cp_ID) AND".$sqltxt." googlemaps_status=1 ORDER BY googlemaps_title";  
}
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

//header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
 if (version_compare(phpversion(), "5.0.0") == -1) {
  $newnode = $dom->createElement("marker");
  $newnode->setAttribute("pointid", $row['googlemaps_id']);
  $newnode->setAttribute("name", htmlentities($row['googlemaps_title']));
  $newnode->setAttribute("address", htmlentities($row['googlemaps_searchlisting']));
  $newnode->setAttribute("lat", $row['googlemaps_lat']);
  $newnode->setAttribute("lng", $row['googlemaps_lon']);
  $newnode->setAttribute("distance", $row['distance']);
  $dom->documentElement->appendChild($newnode);
 } else {
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("pointid", $row['googlemaps_id']);
  $newnode->setAttribute("name", htmlentities($row['googlemaps_title']));
  $newnode->setAttribute("address", htmlentities($row['googlemaps_searchlisting']));
  $newnode->setAttribute("lat", $row['googlemaps_lat']);
  $newnode->setAttribute("lng", $row['googlemaps_lon']);
  $newnode->setAttribute("distance", $row['distance']);
 }
}

echo $dom->saveXML();
?>